<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { loginApi } from '@/api/login'
import { useRouter } from 'vue-router'


const router = useRouter()
 // 登录数据
const loginForm = ref({
  username: '',
  password: ''
})

// 登录
const handleLogin = async () => {
  const result = await loginApi(loginForm.value)
  if (result.code) {
    ElMessage.success('登录成功')
    
    // 存储服务端相应的数据 token,登录用户的信息 - localStorage
    localStorage.setItem('token', result.data.token)
    
    // JSON.stringify() 将对象转换为JSON字符串,JSON.parse() 将JSON字符串转换为对象
    localStorage.setItem('userInfo', JSON.stringify(result.data))
    router.push('/home')
  } else {
    ElMessage.error(result.msg) 
  }
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <div class="system-title">轻客管家</div>
      <el-form :model="loginForm" >
        <el-form-item label="用户名">
          <el-input v-model="loginForm.username" placeholder="请输入用户名">
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="密&nbsp;&nbsp;&nbsp;&nbsp;码">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码">
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-btn" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url('@/assets/bg.jpg') center center / cover no-repeat;
  position: relative;
  overflow: hidden;
}

.login-container::before {
  content: '';
  position: absolute;
  width: 200%;
  height: 200%;
  top: -50%;
  left: -50%;
  background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 60%);
  animation: rotate 30s linear infinite;
}

.system-title {
  font-family: 华文隶书;
  font-size: 50px;
  text-align: center;
  margin-bottom: 30px;
}

@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.login-box {
  width: 450px;
  padding: 40px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  position: relative;
  z-index: 1;
}

.el-input {
  margin-bottom: 20px;
  width: 100%;
}

.el-form-item:last-child {
  margin-bottom: 0;
}
.el-button {
  width: 100%;
  height: 40px;
  font-size: 16px;
  background: linear-gradient(90deg, #1890ff, #36cfc9);
  border: none;
  transition: transform 0.3s ease;
}

.el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
}

.login-btn {
  width: 100%;
  margin-top: 10px;
}

</style>